const app = getApp()
import { list, close, remove, receive } from '../../../api/order'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    header: app.global.headerHeight,
    theme: '',
    back: true,
    title: '我的订单',
    loading: true,
    loadmore: false,
    list: null,
    form: {
      page: 1,
      limit: 10,
      status: 0
    },
    abnor: {
      title: '没找到相关订单哦~',
      button: ''
    }
  },
  onLoad (options) {
    this.data.form.status = parseInt(options.status)
    wx.setStorageSync('order_status', this.data.form.status)
    this.setData({ loading: true, theme: app.global.theme })
  },
  onShow () {
    this.data.list = null
    this.getList()
  },
  onPullDownRefresh () { /* 监听用户下拉动作*/ },
  onReachBottom () {
    if (this.data.form.page == this.data.form.last_page) {
      this.setData({ loadmore: true })
    } else {
      this.data.form.page += 1
      this.getList()
    }
  },

  // ===================methon================

  getList (res) {
    list(this.data.form).then(res => {
      const { data } = res
      this.data.list || (this.data.list = [])
      const list = this.data.list.concat(data.data)
      this.data.form.last_page = res.data.last_page
      this.setData({
        list: list,
        loadmore: data.last_page <= 1
      })
    })
      .finally(() => {
        this.setData({ loading: false })
        wx.hideLoading()
      })
  },

  onChange (event) {
    wx.setStorageSync('order_status', event.detail.index)
    let form = this.data.form
    form.status = event.detail.index;
    form.page = 1
    this.setData({
      list: null,
      form: form
    });
    wx.showLoading({ title: '加载中' });
    this.getList()
  },

  handleClose (event) {
    const that = this
    Dialog.confirm({
      message: '确定取消吗？'
    }).then(() => {
      const id = event.currentTarget.dataset.id
      const list = this.data.list
      close(id).then(res => {
        app.toast('订单已关闭', function () {
          list.forEach(item => {
            if (item.id == id) {
              item.order_status.value = -1
              item.order_status.text = '已关闭'
            }
          })
          that.setData({ list: list })
        }, 'success')
      })
    }).catch(() => { })
  },

  handleReceive (event) {
    const id = event.currentTarget.dataset.id
    receive(id).then(res => {
      app.toast('订单签收成功', function () {
        app.redirect('/pages/order/comment/index?id=' + id)
      }, 'success')
    })
  },

  handleRemove (event) {
    const that = this
    Dialog.confirm({
      message: '确定删除吗？'
    }).then(() => {
      const id = event.currentTarget.dataset.id
      const list = this.data.list
      remove(id).then(res => {
        app.toast('订单已删除', function () {
          list.forEach((item, index) => {
            if (item.id == id) {
              list.splice(index, 1)
            }
          })
          that.setData({ list: list })
        }, 'success')
      })
    }).catch(() => { })
  },

  handlePay (event) {
    app.redirect('/pages/order/detail/index?id=' + event.currentTarget.dataset.id)
  },

  detail (event) {
    app.redirect('/pages/order/detail/index?id=' + event.currentTarget.dataset.id)
  },

  goodsDetail (event) {
    app.redirect('/pages/goods/detail/index?id=' + event.currentTarget.dataset.id)
  },

  redirect (event) {
    app.redirect('/pages/order/comment/index?id=' + event.currentTarget.dataset.id)
  }
})